/**
 * 
 */
package com.jszw.bcp.demo;

import java.lang.reflect.Method;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jszw.bcp.kernal.common.InvokeResult;
import com.jszw.bcp.kernal.log.WebLog;

/**
 * @author zhengzw
 *
 */
@WebLog
public class BaseController implements InitializingBean {

	@Override
	public void afterPropertiesSet() throws Exception {
		Class<? extends BaseController> clazz = this.getClass();
		checkMethodReturnType(clazz);
	}

	/**
	 * 检查Controller方法返回类型
	 * @param clazz
	 */
	private void checkMethodReturnType(Class<? extends BaseController> clazz) {
		// 1、检查Controller的返回类型必须为指定的一个类型
		Method[] methods = clazz.getDeclaredMethods();

		for (Method method : methods) {
			RequestMapping rm = method.getAnnotation(RequestMapping.class);

			if (null != rm) {
				Class<?> rtn = method.getReturnType();
				if (rtn != InvokeResult.class) {
					throw new RuntimeException(
							"方法 [" + clazz.getName() + "." + method.getName() + "] 返回类型必须为InvokeResult");
				}
			}
		}
	}
}
